﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.Model;
using Wsitech.BLL;
public partial class News_VideoDetail : System.Web.UI.Page
{
    protected readonly string tid = HttpContext.Current.Request["tid"] ?? "";

    /// <summary>
    /// 文字信息
    /// </summary>
    public TextInfo Info
    {
        get
        {
            return TextInfoBLL.Instance.GetDetail(tid);
        }
    }

    /// <summary>
    /// 视频段落
    /// </summary>
    public int Paragraph
    {
        get
        {
            int p = 0;
            if (Request["paragraph"] != null)
            {
                try
                {
                    p = int.Parse(Request["paragraph"]) - 1;
                }
                catch (Exception)
                {
                }
            }
            return p;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Info != null)
            {
                SiteMapInfo map = SiteMapInfoBLL.Instance.GetDetail(Info.SitemapId);
                if (map != null)
                {
                    hlVideoIndex.NavigateUrl = string.Format("Video.aspx?sitemapid={0}", map.ParentId);
                    hlVideo.NavigateUrl = string.Format("Video.aspx?sitemapid={0}&subid={1}", map.ParentId, map.SitemapId);
                    hlVideo.Text = map.SitemapName;
                }
                //计数+1
                TextInfo info = TextInfoBLL.Instance.GetDetail(tid);
                info.ViewCount += 1;
                TextInfoBLL.Instance.Update(info);

            }

            bindRelated();
        }
    }

    private void bindRelated()
    {
        if (Info != null)
        {
            //获取相关视频
            IList<TextInfo> list = TextInfoBLL.Instance.GetRelatedVideo(Info);

            if (list.Count < 4)
            {
                int count = 0;
                IList<TextInfo> defaultList = TextInfoBLL.Instance.GetActivePagedListBySiteMapID(Info.SitemapId, 1, 4, ref count);
                foreach (TextInfo item in defaultList)
                {
                    if (!list.Select(t => t.TextInfoId).Contains(item.TextInfoId))
                    {
                        if (item.TextInfoId != Info.TextInfoId)
                        {
                            list.Add(item);
                        }
                    }
                    if (list.Count == 4)
                    {
                        break;
                    }
                }
            }
            repRelated.DataSource = list;
            repRelated.DataBind();
        }
    }

}